package com.weme.common.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.media3.ui.PlayerView
import com.weme.common.util.fadeOut
import com.weme.common.util.gone
import com.weme.common.util.visible
import com.weme.common.databinding.ViewVideoBinding

class MyStyledPlayerView @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defStyle: Int = 0) : FrameLayout(context, attributeSet, defStyle) {
    private val viewBinding = ViewVideoBinding.inflate(LayoutInflater.from(context), this, true)
 //   var videoUri = ""

     var videoUri = mutableListOf<String>()

    init {
        videoUri.clear()
        stopVideo()
        viewBinding.vvBack.player?.stop()


//        player?.addListener(object : Player.Listener {
//            override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
//                if (playbackState == Player.STATE_ENDED) { // Video playback has ended, show the image
//                    showImage()
//                } else if (playbackState == Player.STATE_READY && playWhenReady) { // Video playback has started, hide the image
//
//                }
//            }
//        })
    }

    fun addVideoUrl(videoUrl: String) {
        videoUri.clear()
        this.videoUri.add(videoUrl)

    }
    fun removeVideoView(){
        viewBinding.root.removeView(viewBinding.vvBack)
    }


    fun isPlay(): Boolean {
        return viewBinding.vvBack.player != null && viewBinding.vvBack.player!!.isPlaying
    }

    fun getImageView(): ImageView {
        return viewBinding.imBack
    }
    fun getVideoView(): PlayerView {
        return viewBinding.vvBack
    }





    fun startVideo() {

        Log.e("tag","显示新的视频")
        viewBinding.vvBack.visible()

        Log.e("tag","隐藏新的图片")
        viewBinding.imBack.fadeOut()
    }

    fun stopVideo() {

        Log.e("tag","隐藏旧视频")
        viewBinding.vvBack.gone()
        Log.e("tag","显示旧的图片")
      //  viewBinding.imBack.visibility = VISIBLE
        viewBinding.imBack.alpha = 1f
    }

    fun releasePlayer() {
      //  player?.release()
    }

}